// pages/home/statistics/statistics.js
import {
      Statistics
} from 'statistics-model.js';
import {
      Config
} from '../../../utils/config.js';
var Function = require("../../../utils/function.js");
var api = new Statistics(); //实例化 首页 对象
var getUserInfo = require('../../auth/auth.js')
var App = getApp();
Page({

      /**
       * 页面的初始数据
       */
      data: {
            list: [],
            user:[],
            date: ''
      },

      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {
        let _this = this;
        _this.setData({
          isLogin: App.checkIsLogin()
        });
        if (_this.data.isLogin === false) {
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
      },
      getStatic() {
            var date = this.data.date;
            api.getStatic({
                  date: date
            }, (res) => {
                  if(res.code==0){
                        App.showError(res.msg, function () {
                          wx.navigateBack();
                        });
                      }
                
                  this.setData({
                        list: res.data.list,
                        user:res.data.user
                  })
            })
      },

      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function () {
       this.dataData();
        this.getStatic();
      },

      dataData: function () {
            let time = new Date();
            let year = time.getFullYear();
            let mouth = time.getMonth() + 1
            this.setData({
                  year: year,
                  mouth: mouth
            })
      },
      mouth: function (e) {
            let time = new Date();
            let newYear = time.getFullYear();
            let newMouth = time.getMonth() + 1
            let type = Function.getDataSet(e, 'type')
            let year = this.data.year;
            let mouth = this.data.mouth;
            if (type == 1) {
                  mouth--
                  if (mouth < 1) {
                        mouth = 12
                        year--
                  }
            } else {
                  mouth++
                  if (mouth > 12) {
                        mouth = 1
                        year++
                  }
            }
            if (newYear == year && newMouth < mouth) {
                  Function.layer('不能选择未来的日子');
                  return
            }

            this.setData({
                  year: year,
                  mouth: mouth,
                  date:year+'-'+mouth
            })
            this.getStatic();
      },

      loock(e) {
            let list = this.data.list
            let idx = Function.getDataSet(e, 'idx');
            list[idx].show = !list[idx].show;
            this.setData({
                  list: list
            })
      },

      /**
       * 生命周期函数--监听页面隐藏
       */
      onHide: function () {

      },

      /**
       * 生命周期函数--监听页面卸载
       */
      onUnload: function () {

      },

      /**
       * 页面相关事件处理函数--监听用户下拉动作
       */
      onPullDownRefresh: function () {

      },

      /**
       * 页面上拉触底事件的处理函数
       */
      onReachBottom: function () {

      },

      /**
       * 用户点击右上角分享
       */
      onShareAppMessage: function () {

      }
})